package com.cloud.zgenerator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.*;

public class CodeGenerator {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setAuthor("liuyuan");
        gc.setOpen(false);


        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/cloud_class?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        String scanner = scanner("模块名");
        packageConfig.setParent("com.cloud");
        packageConfig.setController("gateway.controller.admin."+scanner);
        packageConfig.setMapper("server.mapper."+scanner);
        packageConfig.setService("server.service."+scanner);
        packageConfig.setServiceImpl("server.service."+scanner+".impl");
        packageConfig.setEntity("server.domain."+scanner);
        packageConfig.setXml("server.mapper."+scanner);
        mpg.setPackageInfo(packageConfig);

        // 注入自定义配置，可以在 VM 中使用 cfg.abc 【可无】
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };
        mpg.setCfg(injectionConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        String table = scanner("表名，多个英文逗号分割");
        strategy.setInclude(table.split(","));
        //strategy.setSuperEntityClass("com.cloud.common.BaseDomain");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
        //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        // 写于父类中的公共字段
        //strategy.setSuperEntityColumns("id");
        //strategy.set
        strategy.setEntityBooleanColumnRemoveIsPrefix(true);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(packageConfig.getModuleName() + "_");
        mpg.setStrategy(strategy);

        String entity = "";
        ArrayList<String> entitys = new ArrayList<>();
        String[] tables = table.split("_");
        for (int i= 0; i<tables.length; i++){
            entitys.add(tables[i]);
        }
        entitys.remove(0);
        for (String e: entitys) {
            String tableIndex1 = e.substring(0, 1);
            String tableIndex1toUpper = tableIndex1.toUpperCase();
            String replace = e.replace(tableIndex1, tableIndex1toUpper);
            entity = entity + replace;
        }
        //String substring = entity.substring(0, 1);
        //生成文件名:
        gc.setEntityName(entity);
        gc.setXmlName(entity+"Mapper");
        gc.setMapperName(entity+"Mapper");
        gc.setServiceName(entity+"Service");
        gc.setServiceImplName(entity+"ServiceImpl");
        gc.setControllerName(entity+"Controller");
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);
       // mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();


    }
}
